#include <stdio.h>
#include <ctype.h>
#include <stdarg.h>

void minscanf(char *fmt, ...);

int main(void)
{
	int i;
	unsigned u;
	char c;
	char s[10];
	float f;

	minscanf("%d %c %u %s %f", &i, &c, &u, s, &f);
	printf("%d %c %u %s %f\n", i, c, u, s, f);

	return 0;
}

void minscanf(char *fmt, ...)
{
	va_list ap;
	char pfmt[50];
	char *p, *sval;
	int *ival;
	double *dval;
	unsigned *uval;

	va_start(ap, fmt);
	for(p = fmt; *p; p++)
	{
		if(isspace(*p))
		{
			getchar();
			continue;	
		}
		if(*p != '%')
		{
			printf("error: invalid parameter\n");
			return ;
		}
		
		int i;
		for(i = 0; i < 50 && !isalpha(*p); i++, p++)	// 将%以及其后的非字母字符储存在数组中
			pfmt[i] = *p;
		pfmt[i++] = *p;		// 储存第一个字母字符
		pfmt[i] = '\0';

		switch(*p)
		{
		case 'i':
		case 'd':
 		case 'o':
		case 'x':
			ival = va_arg(ap, int *);
			scanf(pfmt, ival);
			break;
		case 'u':
			uval = va_arg(ap, unsigned *);
			scanf(pfmt, uval);
			break;
		case 'f':
		case 'e':
		case 'g':
			dval = va_arg(ap, double *);
			scanf(pfmt, dval);
			break;
		case 'c':
		case 's':
			sval = va_arg(ap, char *);
			scanf(pfmt, sval);
			break;
		default:
			printf("%s\n", pfmt);
			break;
		}
	}
	va_end(ap);
}
